package test.marui.com.mytest.view.layerView.view;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

/**
 * Created by MR on 2019/1/27.
 */
public class AnimHelper {

    public static void startTopInAnim(View target, long duration) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(target, "translationY", -target.getBottom(), 0);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.setDuration(duration);
        animator.start();
    }

    public static void startTopOutAnim(View target, long duration) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(target, "translationY", 0, -target.getBottom());
        animator.setInterpolator(new DecelerateInterpolator());
        animator.setDuration(duration);
        animator.start();
    }

    public static void startZoomInAnim(View targetView, long duration) {
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(targetView, "alpha", 0, 1);
        ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(targetView, "scaleX", 0.618f, 1);
        ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(targetView, "scaleY", 0.618f, 1);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(alphaAnimator, scaleXAnimator, scaleYAnimator);
        set.setInterpolator(new DecelerateInterpolator());
        set.setDuration(duration);
        set.start();

    }

    public static void startZoomOutAnim(View targetView, long duration) {
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(targetView, "alpha", targetView.getAlpha(), 0);
        ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(targetView, "scaleX", targetView.getScaleX(), 0.618f);
        ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(targetView, "scaleY", targetView.getScaleY(), 0.618f);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(alphaAnimator, scaleXAnimator, scaleYAnimator);
        set.setInterpolator(new DecelerateInterpolator());
        set.setDuration(duration);
        set.start();

    }

    public static void startAlphaOutAnim(ImageView view, long duration) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", view.getAlpha(), 0);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.setDuration(duration);
        animator.start();
    }

    public static void startAlphaInAnim(ImageView view, long duration) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 0, 1);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.setDuration(duration);
        animator.start();
    }

    public static void startBottomInAnim(View view, long duration) {
        int y = ((ViewGroup) view.getParent()).getMeasuredHeight() - view.getTop();
        Log.e("iny", String.valueOf(y));
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", y, 0);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.setDuration(duration);
        animator.start();
    }

    public static void startBottomOutAnim(View view, long duration) {
        Log.e("getTranslationY", String.valueOf(view.getTranslationY()));
        int y = ((ViewGroup) view.getParent()).getMeasuredHeight() - view.getTop();
        Log.e("y", String.valueOf(y));
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", view.getTranslationY(), y);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.setDuration(duration);
        animator.start();
    }

    public static void startLeftInAnim(View view, long duration) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", -view.getRight(), 0);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.setDuration(duration);
        animator.start();
    }

    public static void startLeftOutAnim(View view, long duration) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", view.getTranslationX(), -view.getRight());
        animator.setInterpolator(new DecelerateInterpolator());
        animator.setDuration(duration);
        animator.start();
    }

    public static void startRightInAnim(View view, long duration) {
        int x = ((ViewGroup) view.getParent()).getMeasuredWidth() - view.getLeft();
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", x, 0);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.setDuration(duration);
        animator.start();
    }

    public static void startRightOutAnim(View view, long duration) {
        int x = ((ViewGroup) view.getParent()).getMeasuredWidth() - view.getLeft();
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", view.getTranslationX(), x);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.setDuration(duration);
        animator.start();
    }

    public static void startRightAlphaInAnim(View view, long duration) {
        float x = (1 - 0.618f) * view.getMeasuredWidth();
        ObjectAnimator translationXAnimator = ObjectAnimator.ofFloat(view, "translationX", x, 0);
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(view, "alpha", 0.618f, 1f);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(translationXAnimator, alphaAnimator);
        set.setInterpolator(new DecelerateInterpolator());
        set.setDuration(duration);
        set.start();
    }

    public static void startRightAlphaOutAnim(View view, long duration) {
        float x = (view.getAlpha() - 0.618f) * view.getMeasuredWidth();
        ObjectAnimator translationXAnimator = ObjectAnimator.ofFloat(view, "translationX", 0, x);
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(view, "alpha", view.getAlpha(), 0f);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(translationXAnimator, alphaAnimator);
        set.setInterpolator(new DecelerateInterpolator());
        set.setDuration(duration);
        set.start();
    }


    public static void startLeftAlphaInAnim(View view, long duration) {
        float x = (0.618f - 1) * view.getMeasuredWidth();
        ObjectAnimator translationXAnimator = ObjectAnimator.ofFloat(view, "translationX", x, 0);
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(view, "alpha", 0.618f, 1f);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(translationXAnimator, alphaAnimator);
        set.setInterpolator(new DecelerateInterpolator());
        set.setDuration(duration);
        set.start();
    }

    public static void startLeftAlphaOutAnim(View view, long duration) {
        float x = (0.618f - view.getAlpha()) * view.getMeasuredWidth();
        ObjectAnimator translationXAnimator = ObjectAnimator.ofFloat(view, "translationX", 0, x);
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(view, "alpha", view.getAlpha(), 0f);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(translationXAnimator, alphaAnimator);
        set.setInterpolator(new DecelerateInterpolator());
        set.setDuration(duration);
        set.start();
    }

    public static void startBottomAlphaInAnim(View view, long duration) {
        float y = (1 - 0.618f) * ((ViewGroup) view.getParent()).getMeasuredHeight();
        ObjectAnimator translationYAnimator = ObjectAnimator.ofFloat(view, "translationY", y, 0);
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(view, "alpha", 0.618f, 1);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(translationYAnimator, alphaAnimator);
        set.setInterpolator(new DecelerateInterpolator());
        set.setDuration(duration);
        set.start();
    }

    public static void startBottomAlphaOutAnim(View view, long duration) {
        float y = (view.getAlpha() - 0.618f) * ((ViewGroup) view.getParent()).getMeasuredHeight();
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", view.getTranslationY(), y);
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(view, "alpha", view.getAlpha(), 0);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(animator, alphaAnimator);
        set.setInterpolator(new DecelerateInterpolator());
        set.setDuration(duration);
        set.start();
    }

    public static void startTopAlphaInAnim(View target, long duration) {
        float y = (1 - 0.618f) * (((ViewGroup) target.getParent()).getMeasuredHeight());
        ObjectAnimator animator = ObjectAnimator.ofFloat(target, "translationY", -y, 0);
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(target, "alpha", 0.618f, 1f);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(animator, alphaAnimator);
        set.setInterpolator(new DecelerateInterpolator());
        set.setDuration(duration);
        set.start();
    }

    public static void startTopAlphaOutAnim(View target, long duration) {
        float y = (0.618f - target.getAlpha()) * (((ViewGroup) target.getParent()).getMeasuredHeight());
        ObjectAnimator animator = ObjectAnimator.ofFloat(target, "translationY", 0, y);
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(target, "alpha", target.getAlpha(), 0);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(animator, alphaAnimator);
        set.setInterpolator(new DecelerateInterpolator());
        set.setDuration(duration);
        set.start();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public static void startCircleInAnim(View target, long duration) {
        int width = target.getMeasuredWidth();
        int height = target.getMeasuredHeight();
        float r = (float) ((Math.sqrt(width * width + height * height)));
        Animator animator = ViewAnimationUtils.createCircularReveal(target, width / 2, height / 2, 0, r);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.setDuration(duration);
        animator.start();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public static void startCircleOutAnim(View target, long duration) {
        int width = target.getMeasuredWidth();
        int height = target.getMeasuredHeight();
        float r = (float) ((Math.sqrt(width * width + height * height)));
        Animator animator = ViewAnimationUtils.createCircularReveal(target, width / 2, height / 2, r, 0);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.setDuration(duration);
        animator.start();
    }

}
